

package projectx.core.base;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

public final class ResourceUtil {


    public static InputStream getResourceAsStream( String fileName )
        throws ProjectXFileNotFoundException
    {
        InputStream in = getLoader().getResourceAsStream( fileName );

        checkResource( in, fileName );

        return in;
    }


    public static URL getResource( String fileName )
        throws ProjectXFileNotFoundException
    {
        URL url = getLoader().getResource( fileName );

        checkResource( url, fileName );

        return url;
    }

    public static Properties loadProperties( String fileName )
        throws ProjectXSystemException
    {
        return loadProperties( fileName, false );
    }

    public static Properties loadProperties( String fileName, boolean isNative )
        throws ProjectXSystemException
    {
        return isNative ? loadNativeProperties( fileName ) : loadAsciiProperties( fileName );
    }

    private static ClassLoader getLoader(){

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if ( classLoader == null ){
            classLoader = ResourceUtil.class.getClassLoader();
        }

        return classLoader;
    }

    private static void checkResource( Object obj, String fileName )
        throws ProjectXFileNotFoundException
    {
        if ( obj == null ){
            throw new ProjectXFileNotFoundException( fileName );
        }
    }

    private static Properties loadNativeProperties( String fileName )
        throws ProjectXSystemException
    {
        BufferedReader  reader = null;

        try {

            reader = new BufferedReader( new InputStreamReader( getResourceAsStream( fileName ) ) );

            Properties  prop = new Properties();

            String  readLine;
            while ( ( readLine = reader.readLine() ) != null ){

                String  buf = readLine.trim();

                if ( buf.length() == 0 || buf.charAt( 0 ) == '#' || buf.charAt( 0 ) == '!' ){
                    continue;
                }

                int index = buf.indexOf( "=" );
                if ( index < 0 ){
                    // nop;
                }
                else if ( index == buf.length() - 1 ){
                    prop.setProperty( buf.substring( 0, index ).trim(), "" );
                }
                else {
                    prop.setProperty( buf.substring( 0, index ).trim(), buf.substring( index+1 ).trim() );
                }
            }

            return prop;
        }
        catch ( ProjectXFileNotFoundException e ){
            throw e;
        }
        catch ( IOException e ){
            throw new ProjectXIOException( e );
        }
        finally {
            if ( reader != null ) try { reader.close(); } catch ( IOException e ){}
        }
    }

    private static Properties loadAsciiProperties( String fileName )
        throws ProjectXSystemException
    {
        BufferedInputStream ins = null;

        try {

            ins = new BufferedInputStream( getResourceAsStream( fileName ) );

            Properties  prop = new Properties();

            prop.load( ins );

            return prop;
        }
        catch ( ProjectXFileNotFoundException e ){
            throw e;
        }
        catch ( IOException e ){
            throw new ProjectXIOException( e );
        }
        finally {
            if ( ins != null ) try { ins.close(); } catch ( IOException e ){}
        }
    }
    private ResourceUtil(){}
}
